package cc.admin.common.db.table.reader;

import cc.admin.common.db.table.base.EtlReader;
import cc.admin.common.db.table.entity.DataReader;
import cc.admin.common.db.table.entity.MetaSql;
import cc.admin.common.db.util.DbConstants;
import cc.admin.common.exception.CcAdminException;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
/**
 * @author: ZhangHouYing
 * @date: 2018-06-24
 */
@Slf4j
public class OracleReader extends EtlReader {

	/**
	 * table_name
	 */
	private String tableSql = "select table_name from all_tables";
	/**
	 * table_name ,comments
	 */
	private String tableCommentSql = "select table_name ,comments from all_tab_comments";
	/**
	 * 对于oracle来说number类型 有效位数为 data_precision  不是data_length
	 * table_name,column_name,data_type,data_length,data_precision,data_scale,nullable
	 */
	private String columnSql = "select table_name,column_name,data_type,data_length,data_precision,data_scale,nullable from all_tab_columns";
	/**
	 * table_name,column_name,comments
	 */
	private String columnCommentSql = "select table_name,column_name,comments from all_col_comments";
	/**
	 * table_name,column_name
	 */
	private String pkSql = "select b.table_name,b.column_name from user_constraints a, user_cons_columns b　 where a.constraint_type = 'P' and a.constraint_name = b.constraint_name and a.owner = b.owner  and a.table_name = b.table_name";

	@Override
	protected String getDbNameSql() {
		return "select name,dbid from v$database";
	}

	/**
	 * 获取某个用户下指定表结构
	 */
	@Override
	public void initAllSql() {
		if (StrUtil.isNotEmpty(owner)) {
			this.tableSql += " where lower(owner) = lower('" + owner + "')";
			this.tableCommentSql += " where lower(owner) = lower('" + owner + "')";
			this.columnSql += " where lower(owner) = lower('" + owner + "')";
			this.columnCommentSql += " where lower(owner) = lower('" + owner + "')";
			this.pkSql += " and lower(a.owner) = lower('" + owner + "')";
			if (StrUtil.isNotEmpty(tableName)) {
				this.tableSql += " and lower(table_name)=lower('" + tableName + "')";
				this.tableCommentSql += " and lower(table_name)=lower('" + tableName + "')";
				this.columnSql += " and lower(table_name)=lower('" + tableName + "')";
				this.columnCommentSql += " and lower(table_name)=lower('" + tableName + "')";
				this.pkSql += " and lower(a.table_name)=lower('" + tableName + "')";
			}
		}
		this.metaSql = new MetaSql(tableSql, tableCommentSql, columnSql, columnCommentSql, pkSql);
	}

	@Override
	public String getPageDataSql(DataReader dataReader) {
		StringBuilder sb = new StringBuilder();
		if (StrUtil.isNotEmpty(dataReader.getCnd())) {
			sb.append("and ").append(dataReader.getCnd());
		}
		StringBuilder orderBy = new StringBuilder();
		if (StrUtil.isNotEmpty(dataReader.getOrderBy())) {
			orderBy.append(" order by ").append(dataReader.getOrderBy());
		}
		String sql = String.format("select * from (select rownum as %s, t.* from %s t where  rownum <= %d %s) table_alias where table_alias.%s > %d", DbConstants.DB_ROW_NUM, dataReader.getTableName(), dataReader.getPageNo() * dataReader.getPageSize(), sb.toString(), DbConstants.DB_ROW_NUM, (dataReader.getPageNo() - 1) * dataReader.getPageSize());
		log.info("read sql:{}", sql);
		return sql;
	}

	@Override
	public String getPageDataSql(String sql, int pageNo, int pageSize) {
		CcAdminException.runTimeException("oracle 没实现这个方法");
		return null;
	}

}
